package com.hfzy.ihk.web.freeswitchManage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hfzy.ihk.facade.freeswitchManage.entity.SimCard;
import com.hfzy.ihk.web.freeswitchManage.mapper.SimCardMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by czs on 2019/4/23.
 */
@RestController
@RequestMapping("clearFsNum")
@Slf4j
public class ClearFsNumController {

    @Autowired
    SimCardMapper simCardMapper ;

    @RequestMapping("queryAllNum")
    public Map<String,Object> queryAllNum(){
        Map<String,Object> map  = new HashMap<>();
        try {
            List<SimCard> simCards = simCardMapper.selectAll();
            map.put("code","1");
            map.put("data",simCards);
        }catch (Exception e){
            log.error(">>>查询所有卡槽号码异常",e);
            map.put("code","0");
        }
        return map;

    }


    @RequestMapping("fsClearResult")
    public String fsClearResult(String data){
        log.info("接受到fs洗号结果data:{}",data);
        if(data==null) {
            log.info("接受到fs洗号结果为空");
            return "-1";
        }

        JSONArray jsonArray = JSON.parseArray(data);
        if(jsonArray.size()==0)
            return "-1";
        try {
            for(int i=0;i<jsonArray.size();i++){
                String phone = jsonArray.getJSONObject(i).getString("phone");
                String type = jsonArray.getJSONObject(i).getString("type");
                simCardMapper.updateState(phone, type);
            }
        }catch (Exception e){
            log.error(">>fs 洗号更新失败");
        }
        return "1";
    }
}
